//
//  YTExpertListViewController.m
//  YuTang
//
//  Created by 葛悦彣 on 2024/11/20.
//

#import "YTExpertListViewController.h"
#import "YTExpertModel.h"
#import "YTExpertTableViewCell.h"
#import "YCMenuView.h"
#import "YTTipsShowView.h"
@interface YTExpertListViewController ()
@property (nonatomic ,strong) NSMutableArray<YTExpertModel *> * modelArray;
@property (nonatomic ,strong) NSURLSessionDataTask * task;
@end

@implementation YTExpertListViewController
-(NSMutableArray <YTExpertModel *>*)modelArray{
    if(!_modelArray){
        _modelArray = [NSMutableArray array];
    }
    return _modelArray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.view addSubview:self.tableview];
    self.tableview.backgroundColor = KcolorHex(@"#ECEDEE");
    [self.tableview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.bottom.offset(0);
    }];
    MJWeakSelf
    [self addHeaderView:^{
        [weakSelf refreshingData];
    }];
    [self addFooterView:^{
        [weakSelf refreshingData];
    }];
    
}

-(void)addMoreData{
    self.pageNo += 1;
    [self loadNewData];
}

- (void)refreshingData{
    self.pageNo = 1;
//    [self.modelArray removeAllObjects];
    [self loadNewData];
}

- (void)loadNewData{
    if(self.task){
        [self.task cancel];
        [self.view hideBusyHUD];
    }
    ///走2个接口一个1级列表和推荐
    [self.view showBusyHUD];
    self.task = [SCNetManger expertGetAppExpertListWithDict:[NSMutableDictionary dictionary] andCompetionHandlerBlock:^(HTTPArrBase * model) {
        self.task = nil;
        [self.view hideBusyHUD];
        if(model.code == 200){
            NSArray * array = [NSArray yy_modelArrayWithClass:YTExpertModel.class json:model.result];
            [self.tableview.mj_header endRefreshing];
            if(array.count > 0 && self.self.modelArray.count > 0){
                [self.tableview.mj_footer endRefreshingWithNoMoreData];
            } else {
                [self.tableview.mj_footer endRefreshing];

            }
            self.modelArray = [NSMutableArray arrayWithArray:array];
         
            [self.tableview reloadData];
        } else {
            [self.view showWarning:model.message];
            
        }
        
    }];
    
    
    
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return  kwt(10);
}

- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.modelArray.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return self.modelArray[indexPath.row].CellH;
}

- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    YTExpertTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"YTExpertTableViewCell"];
    if (cell == nil) {
        cell = [[YTExpertTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"YTExpertTableViewCell"];
    }
    if(self.modelArray.count > indexPath.row){
        cell.model = self.modelArray[indexPath.row];
    }
    MJWeakSelf
    cell.openSQBlock = ^(UIView * _Nonnull view, YTExpertModel * _Nonnull model) {
        
        NSMutableArray * array = [NSMutableArray array];
        if(model.ifAuthorize){
            YCMenuAction * m = [YCMenuAction actionWithTitle: @"查看授权码" image:nil handler:^(YCMenuAction *action) {
                [weakSelf CKSQM:model];
            }];
            [array addObject:m];
            YCMenuAction * m1 = [YCMenuAction actionWithTitle: @"取消授权" image:nil handler:^(YCMenuAction *action) {
                [weakSelf QXSQ:model];
            }];
            [array addObject:m1];
        } else {
            YCMenuAction * m = [YCMenuAction actionWithTitle: @"确定授权" image:nil handler:^(YCMenuAction *action) {
                [weakSelf QDSQ:model];
            }];
            [array addObject:m];
        }
        YCMenuView * menuview = [YCMenuView menuWithActions:array width:kwt(120) relyonView:view];
        menuview.menuColor = KcolorHex(@"#434343");
        menuview.textColor = UIColor.whiteColor;
        menuview.textFont = kFont(14);
        menuview.separatorColor = KcolorHex(@"#636161");
        menuview.disMissBlock = ^{
            [weakSelf.tableview reloadData];
        };
        [menuview show];
    };
    cell.openPhoneBlock = ^(YTExpertModel * _Nonnull model) {
        [weakSelf expertCallCountWithID:model];
    };
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell;
}

- (void)QDSQ:(YTExpertModel *)model{
    MJWeakSelf
    [YTTipsShowView showviewWithTips:[NSString stringWithFormat:@"确定将养殖场信息\n授权给专家“%@”？",model.realName] andLeftTitle:@"暂不" andLeftBlock:^{} andRightBlock:^{
        [weakSelf expertEmpowerWithID:model.ID];
    }];
}

- (void)QXSQ:(YTExpertModel *)model{
    MJWeakSelf
    [YTTipsShowView showviewWithTips:@"取消授权？" andLeftTitle:@"暂不" andLeftBlock:^{} andRightBlock:^{
        [weakSelf expertCancelEmpowerWithID:model.ID];
    }];
}

- (void)CKSQM:(YTExpertModel *)Model{
    
    [self.view showBusyHUD];
    MJWeakSelf
    [SCNetManger expertLookCodeWithID:Model.ID andCompetionHandlerBlock:^(HTTPStrBase * model) {
        [self.view hideBusyHUD];
        if(model.code == 200){
            NSString * ID = model.result;
            [YTTipsShowView showviewWithTips:[NSString stringWithFormat:@"已授权\n授权码：%@",ID] andLeftTitle:@"复制授权码" andLeftBlock:^{
                UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
                // 设置要复制到剪贴板的字符串
                pasteboard.string = ID;
                [weakSelf.view showWarning:@"已复制"];
            } andRightBlock:^{
            }];
        } else {
            [self.view showWarning:model.message];
        }
    }];
}
- (void)expertEmpowerWithID:(NSString *)ID{
    [self.view showBusyHUD];
    
    [SCNetManger expertEmpowerWithID:ID andCompetionHandlerBlock:^(HTTPArrBase * model) {
        [self.view hideBusyHUD];
        if(model.code == 200){
            [self.view showWarning:model.message];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                // 2秒后执行的代码
                [self refreshingData];
            });
        } else {
            [self.view showWarning:model.message];
        }
    }];
}
- (void)expertCancelEmpowerWithID:(NSString *)ID{
    [self.view showBusyHUD];
    [SCNetManger expertCancelEmpowerWithID:ID andCompetionHandlerBlock:^(HTTPArrBase * model) {
        [self.view hideBusyHUD];
        if(model.code == 200){
            [self.view showWarning:model.message];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                // 2秒后执行的代码
                [self refreshingData];
            });
        } else {
            [self.view showWarning:model.message];
        }
    }];
}
- (void)expertCallCountWithID:(YTExpertModel *)Model{

    [SCNetManger expertCallCountWithID:Model.ID andCompetionHandlerBlock:^(HTTPArrBase * model) {
        [self refreshingData];
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"tel://%@",Model.phone]] options:nil completionHandler:nil];

//        if(model.code == 200){
//            [self.view showWarning:model.message];
//          
//        } else {
//            [self.view showWarning:model.message];
//        }

    }];
}
@end
